/*
 * Copyright (c) 2013-2015 Charkey. All rights reserved.
 *
 * This software is the confidential and proprietary information of Charkey.
 * You shall not disclose such Confidential Information and shall use it only
 * in accordance with the terms of the agreements you entered into with Charkey.
 *
 * Charkey MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE SOFTWARE,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
 *
 * Charkey SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING,
 * MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
 */

package cn.simastudio.talos.core.service.base.impl;

import cn.simastudio.talos.core.service.base.CacheService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.stereotype.Service;

/**
 * Usage: Cache服务类
 *
 * @author Charkey
 * @date 2016/5/18 15:24
 */
@Deprecated
@Service
public class EhCacheServiceImpl implements CacheService {

    @Autowired
    private CacheManager cacheManager;

    @Override
    public Cache getCache(String cacheName) {
        return cacheManager.getCache(cacheName);
    }

    @Override
    public Object get(String cacheName, Object key) {
        Cache cache = this.getCache(cacheName);
        Cache.ValueWrapper element = cache.get(key);
        if (element != null) {
            return element.get();
        }
        return null;
    }

    @Override
    public void put(String cacheName, Object key, Object value) {
        Cache cache = this.getCache(cacheName);
        cache.put(key, value);
    }

    @Override
    public void evict(String cacheName, Object key) {
        Cache cache = this.getCache(cacheName);
        cache.evict(key);
    }

    @Override
    public void clear(String cacheName) {
        this.getCache(cacheName).clear();
    }
}
